package workers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import messaging.IMessagingHandler;
import messaging.JMSMessagingConstants;
import messaging.SoftwareDevelopmentMessagingHandler;
import model.Software;
import workerUIs.SpecificationStepFrame;
import workflow.step.SpecificationStep;

/**
 * SpecificationStepWorker class.
 * 
 * @author Mark Czotter
 * @author Miklos Szennai
 * 
 */
public class SpecificationStepWorker extends AbstractStepSwingWorker {
	
	private final String desiredName;
	
	public SpecificationStepWorker(SpecificationStepFrame frame, IMessagingHandler<Software> messagingHandler) {
		super(frame, messagingHandler);
		this.desiredName = frame.getSoftwareName();
	}
	
	@Override
	protected Software doInBackground() throws Exception {
		try {
			doStep(null);
			this.messagingHandler.put(this.software);
			return this.software;
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Exception when executing step: " + getClass().getName() + ": " + e.getMessage());
		}
		return null;
	}
	
	@Override
	protected void doStep(Software software) {
		this.software = new SpecificationStep().doStep(desiredName);
	}
	
	public static void main(String[] args) {
		Logger logger = Logger.getLogger(SpecificationStepWorker.class.getCanonicalName());
		try {
			final SpecificationStepFrame specFrame = new SpecificationStepFrame("Step1: Specification", /*true*/ false, 0, 200);
			List<String> specOutputQueues = new ArrayList<String>();
			specOutputQueues.add(JMSMessagingConstants.SPEC_OUTPUT_ARCH_INPUT);
			final IMessagingHandler<Software> messagingHandler = new SoftwareDevelopmentMessagingHandler(null, Collections.EMPTY_LIST, specOutputQueues);
			specFrame.getNextButton().addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					new SpecificationStepWorker(specFrame, messagingHandler).execute();				
				}
			});
		}
		catch (Exception e)	{
			logger.log(Level.SEVERE, "Error during initialization: ", e);
		}
	}

}
